

/* MENU LAYOUT
 
 Main Menu - 1
 Set Alarm - 2
 Show Alarm and allow changes and Exit
 Set Time - 3
 Show Time and allow changes and Exit
 
 */

bool menuFlip;
unsigned long drawMenuTick;

void DrawMenu()
{
  if(drawMenuTick == 0 || millis() < drawMenuTick)
  {
    drawMenuTick = millis();
  }
  if (millis() - drawMenuTick >= alarmSetTimeBlinkRate) 
  {
    drawMenuTick = millis();
    menuFlip = !menuFlip;
  }

  if(menu == 1)
  {
    if(option == 0)
    {
      DrawScrollingText("SET ALARMM", 9);
    }
    else
    {
      DrawScrollingText("SET TIMME", 8);
    }
  }
  else if(menu == 2)
  {
    if(menuFlip)
    {
      DrawMenuAlarm();
    }
    else
    {
      ClearScreen();
    }
  }
  else if(menu == 3)
  {
    if(menuFlip)
    {
      DrawClock();
    }
    else
    {
      ClearScreen();
    }
  }
}

int leftCount;
int rightCount;
void Menu()
{
  if(lastMenuTick == 0 || millis() < lastMenuTick)
  {
    lastMenuTick = millis();
  }
  if(showAlarm)
  {
    menu = 0;
    option = 0;
  }

  if (millis() - lastMenuTick >= menuRate && !showAlarm) 
  {
    lastMenuTick = millis();

    if(menu == 0 && shakeActive)
    {
      menu = 1;
      charPos = 0;
      ResetInputs();
    }
    else if(menu == 1)
    {
      if(leftActive)
      {
        if(option == 1)
        {
          charPos = 0;
        }

        option = 0;
        ResetInputs();
      }
      else if(rightActive)
      {
        if(option == 0)
        {
          charPos = 0;
        }
        option = 1;
        ResetInputs();
      }
      if(shakeActive)
      {
        leftCount = rightCount = 0;
        if(option == 0)
        {
          menu = 2;
        }
        if(option == 1)
        {
          menu = 3;
        }
        ResetInputs();
      }
    }
    else if(menu == 2)
    {
      if(leftActive)
      {
        rightCount = 0;
        leftCount++;
        ChangeAlarm(leftCount > 10, true);
        ResetInputs();
      }
      else if(rightActive)
      {
        leftCount = 0;
        rightCount++;
        ChangeAlarm(rightCount > 10, false);
        ResetInputs();
      }
      else if(shakeActive)
      {
        leftCount = rightCount = 0;
        menu = 0;
        ResetInputs();
      }
      else
      {
        leftCount = rightCount = 0;
      }
    }
    else if(menu == 3)
    {
      if(leftActive)
      {
        rightCount = 0;
        leftCount++;
        ChangeTime(leftCount > 10, true);
        ResetInputs();
      }
      else if(rightActive)
      {
        leftCount = 0;
        rightCount++;
        ChangeTime(rightCount > 10, false);
        ResetInputs();
      }
      else if(shakeActive)
      {
        leftCount = rightCount = 0;
        menu = 0;
        ResetInputs();
      }
      else
      {
        leftCount = rightCount = 0;
      }
    }
  }
}

void ChangeTime(bool changeSize, bool changeUp)
{
  if(changeUp)
  {
    minutesLittle++;
    if(changeSize)
    {
      minutesLittle = 10;
    }
    if (minutesLittle >=10) 
    {
      minutesBig++;
      minutesLittle = 0;
      if(minutesBig >= 6)
      {
        hoursLittle++;
        minutesBig = 0; 
        if (hoursLittle >= 10) 
        {
          hoursBig++;
          hoursLittle = 0;
        }
        if(is24Hour && hoursBig >= 2 && hoursLittle >= 5)
        {
          hoursBig = 0;
          hoursLittle = 1;
        }
        else if(!is24Hour && hoursBig >= 1 && hoursLittle >=3)
        {
          hoursBig = 0;
          hoursLittle = 1;
        }
      }
    }
  }
  else
  {
    minutesLittle--;
    if(changeSize)
    {
      minutesLittle = -1;
    }
    if (minutesLittle <=-1) 
    {
      minutesBig--;
      minutesLittle = 9;
      if(minutesBig <= -1)
      {
        hoursLittle--;
        minutesBig = 5; 
        if (hoursLittle <= 0) 
        {
          hoursBig--;
          if(is24Hour)
          {
            if(hoursBig <= -1)
            {
              hoursBig = 2;
            }
            if(hoursBig < 2)
            {
              hoursLittle = 9;
            }
            else
            {
              hoursLittle = 4;
            }
          }
          else
          {
            if(hoursBig <= -1)
            {
              hoursBig = 1;
              hoursLittle = 2;
            }
            else
            {
              hoursLittle = 9;
            }
          }
        }
      }
    }
  }
}

void ChangeAlarm(bool changeSize, bool changeUp)
{
  if(changeUp)
  {
    alarmminutesLittle = alarmminutesLittle + 5;
    if(changeSize)
    {
      alarmminutesLittle = 10;
    }
    if (alarmminutesLittle >=10) 
    {
      alarmminutesBig++;
      alarmminutesLittle = 0;
      if(alarmminutesBig >= 6)
      {
        hoursLittle++;
        alarmminutesBig = 0; 
        if (alarmhoursLittle >= 10) 
        {
          alarmhoursBig++;
          alarmhoursLittle = 0;
        }
        if(is24Hour && alarmhoursBig >= 2 && alarmhoursLittle >= 5)
        {
          alarmhoursBig = 0;
          alarmhoursLittle = 1;
        }
        else if(!is24Hour && alarmhoursBig >= 1 && alarmhoursLittle >=3)
        {
          alarmhoursBig = 0;
          alarmhoursLittle = 1;
        }
      }
    }
  }
  else
  {
    alarmminutesLittle--;
    if(changeSize)
    {
      alarmminutesLittle = -1;
    }
    if (alarmminutesLittle <=-1) 
    {
      alarmminutesBig--;
      alarmminutesLittle = 9;
      if(alarmminutesBig <= -1)
      {
        alarmhoursLittle--;
        alarmminutesBig = 5; 
        if (alarmhoursLittle <= 0) 
        {
          alarmhoursBig--;
          if(is24Hour)
          {
            if(alarmhoursBig <= -1)
            {
              alarmhoursBig = 2;
            }
            if(alarmhoursBig < 2)
            {
              alarmhoursLittle = 9;
            }
            else
            {
              alarmhoursLittle = 4;
            }
          }
          else
          {
            if(alarmhoursBig <= -1)
            {
              alarmhoursBig = 1;
              alarmhoursLittle = 2;
            }
            else
            {
              alarmhoursLittle = 9;
            }
          }
        }
      }
    }
  }
}

